package com.zjw.zy.coreui.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import com.zjw.zy.R
import com.zjw.zy.base.BaseViewGroup
import com.zjw.zy.utils.ResourcesUtils
import com.zjw.zy.utils.TimeUtils
import com.zjw.zy.utils.taskpool.MainExecutor

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/18 9:22
 */
class VoicePlayView : BaseViewGroup {

    private var isPlay = false
    private var mListener: Listener? = null
    private val playButton: ImageView by lazy { findViewById(R.id.voice_btn_play) }
    private val progressBar: ProgressBar by lazy { findViewById(R.id.voice_progress) }
    private val deleteButton: ImageView by lazy { findViewById(R.id.voice_btn_delete) }
    private val toTextButton: ImageView by lazy { findViewById(R.id.voice_btn_tran_text) }
    private val currentDurationTxt: TextView by lazy { findViewById(R.id.voice_current_duration) }
    private val totalDurationTxt: TextView by lazy { findViewById(R.id.voice_total_duration) }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    override fun onLayoutId(): Int {
        return R.layout.voice_play_view
    }

    override fun onBindListener() {
        deleteButton.setOnClickListener {
            mListener?.onDeleteClick()
        }

        toTextButton.setOnClickListener {
            mListener?.onTransToTextClick()
        }

        playButton.setOnClickListener {
            if (isPlay) {
                isPlay = false
                playButton.setImageDrawable(ResourcesUtils.getDrawable(R.drawable.icon_play))
                mListener?.onStopClick()
            } else {
                isPlay = true
                playButton.setImageDrawable(ResourcesUtils.getDrawable(R.drawable.icon_pause))
                mListener?.onPlayClick()
            }
        }
    }

    override fun onBindValue() {
        playButton.setImageDrawable(ResourcesUtils.getDrawable(R.drawable.icon_play))

    }

    override fun onFindView(rootView: View) {}

    fun bindData(currentDuration: Int, totalDuration: Int) {
        currentDurationTxt.text = TimeUtils.parseToStrTime(currentDuration)
        totalDurationTxt.text = TimeUtils.parseToStrTime(totalDuration)
        progressBar.max = totalDuration
        progressBar.setProgress(currentDuration, true)

        if (currentDuration == totalDuration) {
            MainExecutor.get().mainHandler.postDelayed({
                //还原
                isPlay = false
                playButton.setImageDrawable(ResourcesUtils.getDrawable(R.drawable.icon_play))
                progressBar.progress = 0
            }, 1000L)
        }
    }

    fun bindListener(listener: Listener) {
        mListener = listener
    }


    interface Listener {
        fun onDeleteClick()
        fun onPlayClick()
        fun onStopClick()
        fun onTransToTextClick()
    }
}